

# Load data ---------------------------------------------------------------

btw_kreis_harm_53_17 <- read_rds("data/cleaned/btw_all_counties_1953_2017_proj_to_2017.Rds") %>% 
    as_tibble() %>% 
    select(1:4, turnout, ends_with("_zweit_sh"))

treat_parl_validated <- read_csv2("data/cleaned/parl_treat_validated.csv")


# Add covariates ----------------------------------------------------------

all_covariates <- read_rds("data/raw/covariates/kreis_covs.rds") %>% 
    rename(ags_proj = ags) %>% 
    mutate(year = as.character(year))


btw_kreis_harm_53_17 <- left_join(btw_kreis_harm_53_17, all_covariates)

# Assign  Treatment -------------------------------------------------------

btw_kreis_harm_53_17 %<>% 
    mutate(state_id = str_sub(ags_proj, end = 2L) %>% as.numeric,
           year = as.numeric(year))

btw_kreis_harm_53_17 <- purrr::reduce(list(btw_kreis_harm_53_17, treat_parl_validated), left_join)
btw_kreis_harm_53_17 %<>% mutate(across(greens_in_st_parl:greens_exit_st_parl, ~ifelse(is.na(.), 0, . )))

# Add lags for party vars -------------------------------------------------

btw_kreis_harm_53_17 <- btw_kreis_harm_53_17 %>% 
    arrange(year) %>% 
    group_by(ags_proj) %>% 
    mutate(across(turnout:cdu_csu_zweit_sh, ~ dplyr::lag(.x, n = 1L), .names = "{.col}_l1"))

# Add change in DV --------------------------------------------------------

btw_kreis_harm_53_17 %<>% 
    mutate(delta_cdu_csu = cdu_csu_zweit_sh - cdu_csu_zweit_sh_l1,
           delta_turnout = turnout - turnout_l1) 


# Add district Census data ------------------------------------------------

kreis_census <- read_dta("data/raw/ZA2472.dta") %>% 
    mutate(ident = str_sub(ident, end = -4L),
           ident = case_when(nchar(ident) <5 ~ paste0(0, ident), 
                             T ~ paste0(ident))) %>% 
    rename(county_id_old = ident,
           pop_50 = vza003,
           pop_male_50 = vza004,
           pop_over65_50 = vza010,
           pop_protest_50 = vza011,
           pop_catho_50 = vza012,
           erwerb_agri_50 = vza025,
           taxes_thou_dm_50 = vza043,
           pop_61 = vzb001,
           pop_male_61 = vzb002,
           pop_over65_61 = vzb022,
           pop_protest_61 = vzb049,
           pop_catho_61 = vzb050,
           erwerb_agri_61 = vzb086,
           taxes_thou_dm_61 = vzb123,
           pop_70 = vzc001,
           pop_male_70 = vzc002,
           pop_protest_70 = vzc066,
           pop_catho_70 = vzc067,
           pop_students_70 = vzc082,
           erwerb_agri_70 = vzc127,
           taxes_thou_dm_70 = vzc203,
           pop_over6570_70 = vzc222,
           pop_over70_70 = vzc223,
    ) %>% 
    select(county_id_old, 
           starts_with("pop_"),
           starts_with("erwerb_"),
           starts_with("taxes_"))

conv <- read_rds("data/raw/cty_conv.rds")

kreis_census_proj <- left_join(kreis_census, conv) %>% rename(ags_proj = county_id_2018) 
kreis_census_proj_agg <- kreis_census_proj %>%
    group_by(ags_proj) %>%
    summarise(across(-county_id_old, sum)) %>%
    mutate(
        pop_log_50 = log(pop_50),
        pop_sh_male_50 = pop_male_50 / pop_50,
        pop_sh_over65_50 = pop_over65_50 / pop_50,
        pop_sh_catho_50 = pop_catho_50 / pop_50,
        erwerb_sh_agri_50 = erwerb_agri_50 / pop_50,
        taxes_per_cap_50 = taxes_thou_dm_50 / pop_50,
        pop_log_61 = log(pop_61),
        pop_sh_male_61 = pop_male_61 / pop_61,
        pop_sh_over65_61 = pop_over65_61 / pop_61,
        pop_sh_catho_61 = pop_catho_61 / pop_61,
        erwerb_sh_agri_61 = erwerb_agri_61 / pop_61,
        taxes_per_cap_61 = taxes_thou_dm_61 / pop_61,
        pop_log_70 = log(pop_70),
        pop_sh_male_70 = pop_male_70 / pop_70,
        pop_sh_catho_70 = pop_catho_70 / pop_70,
        pop_sh_students_70 = pop_students_70 / pop_70,
        erwerb_sh_agri_70 = erwerb_agri_70 / pop_70,
        taxes_per_cap_70 = taxes_thou_dm_70 / pop_70,
        pop_sh_over65_70 = (pop_over6570_70 + pop_over70_70) / pop_70
    ) %>% 
    select(
        -c(
            "pop_70",
            "pop_61",
            "pop_50",
            "pop_over6570_70",
            "pop_over70_70",
            "pop_over65_61",
            "pop_over65_50",
            "taxes_thou_dm_70",
            "taxes_thou_dm_61",
            "taxes_thou_dm_50",
            "erwerb_agri_70",
            "erwerb_agri_61",
            "erwerb_agri_50",
            "pop_students_70",
            "pop_catho_70",
            "pop_catho_61",
            "pop_catho_50",
            "pop_male_70",
            "pop_male_61",
            "pop_male_50"
        )
    )


btw_kreis_harm_53_17 <- left_join(btw_kreis_harm_53_17, kreis_census_proj_agg)


# Remove unecessary objects -----------------------------------------------

rm(list=setdiff(ls(), "btw_kreis_harm_53_17"))
write_csv(btw_kreis_harm_53_17, "data/cleaned/btw_harm_kreis_53_17_cleaned.csv")
